<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-row style="height: 60px">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="showDialog = true"
              >新增角色</el-button>
            </el-row>
            <el-table border :data="list" style="width: 100%">
              <el-table-column
                label="序号"
                type="index"
                align="center"
                width="120"
              />
              <el-table-column
                label="角色名称"
                prop="name"
                align="center"
                width="240"
              />
              <el-table-column label="描述" align="center" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="onEditRoles(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRoles(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                :current-page.sync="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              :closable="false"
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
            />
            <el-form ref="form" label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 编辑弹出层 -->
    <el-dialog
      :title="roleForm.id ? '编辑角色' : '新增角色'"
      :visible.sync="showDialog"
      width="50%"
      :before-close="btnCancel"
    >
      <el-form
        ref="roleFormRef"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" justify="center" align="middle">
        <el-button size="small" @click="btnCancel">取 消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确 定</el-button>
      </el-row>
    </el-dialog>

    <!-- 分配权限弹层 -->
    <el-dialog title="分配权限" :visible="showAssignDialog" width="50%" @close="btnPermClose">
      <el-tree
        ref="roleTree"
        :data="permData"
        show-checkbox
        :props="{ label: 'name' }"
        default-expand-all
        node-key="id"
        :default-checked-keys="selectCheck"
        check-strictly
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOk">确 定</el-button>
          <el-button
            size="small"
            @click="showAssignDialog = false"
          >取 消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, getCompany, deleteRoles, getRolesInfo, updateRoles, addRole, assignPerm } from '@/api/setting.js'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'SettingIndex',
  components: {

  },
  data() {
    return {
      activeName: 'first',
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      formData: {},
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      },
      showAssignDialog: false,
      permData: [],
      roleId: null,
      selectCheck: []
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  async created() {
    this.getRoles()
    this.formData = await getCompany(this.companyId)
  },
  methods: {
    async getRoles() {
      const { rows, total } = await getRoles(this.page)
      this.page.total = total
      this.list = rows
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getRoles()
    },
    deleteRoles(id) {
      this.$confirm('确认删除该角色?').then(async active => {
        await deleteRoles(id)
        this.getRoles()
        this.$message.success('删除角色成功OVO')
      }).catch(() => {

      })
    },
    async onEditRoles(id) {
      this.roleForm = await getRolesInfo(id)
      this.showDialog = true
    },
    async btnOk() {
      var message = ''
      try {
        await this.$refs.roleFormRef.validate()
        if (this.roleForm.id) {
          // 编辑
          await updateRoles(this.roleForm)
          message = '更新成功'
        } else {
          // 新增
          message = '新增成功'
          await addRole(this.roleForm)
        }
        this.$message.success(message)
        this.getRoles()
        this.showDialog = false
      } catch (error) {
        console.error(error)
      }
    },
    btnCancel() {
      // 手动重置表单(删除id属性)
      this.roleForm = this.$options.data().roleForm
      this.showDialog = false
      // 重置表单验证样式
      this.$refs.roleFormRef.resetFields()
    },
    // 获取权限点并回写
    async assignPerm(id) {
      const list = await getPermissionList(id)
      this.permData = tranListToTreeData(list, '0')
      this.roleId = id
      const { permIds } = await getRolesInfo(id)
      this.selectCheck = permIds
      this.showAssignDialog = true
    },
    // 点击确定分配权限
    btnPermOk() {
      const checkedRole = this.$refs.roleTree.getCheckedKeys()
      assignPerm({ id: this.roleId, permIds: checkedRole })
      this.$message.success('分配权限成功!')
      this.showAssignDialog = false
    },
    // 点击取消
    btnPermClose() {
      this.selectCheck = []
      this.showAssignDialog = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
